﻿/* 12. We are given integer number n, value v (v=0 or 1) and a position p. 
 * Write a sequence of operators that modifies n to hold the value v at the position p from the binary representation of n. 
 * Examples: n = 5 (00000101), p=3, v=1 --> 13 (00001101);    n = 5 (00000101), p=2, v=0 --> 1 (00000001). */

using System;

public class ModifyOneBit
{
    public static void Main()
    {
        Console.Write("n = ");
        int n = int.Parse(Console.ReadLine());
        Console.Write("{0}\np = ", string.Format(Convert.ToString(n, 2).PadLeft(32, '0')));
        byte p = byte.Parse(Console.ReadLine());
        byte v;
        do
        {
            Console.Write("(choose 0 or 1) v = ");
            v = byte.Parse(Console.ReadLine());
        } while((v < 0) || (1 < v));

        if (v == 1)
            n |= (1 << p);
        else
            n &= ~(1 << p);
        
        Console.WriteLine("{0}\n{1}", n, string.Format(Convert.ToString(n, 2).PadLeft(32, '0')));
    }
}